package com.usian.jyzx.user.controller;

import com.alibaba.fastjson.JSON;
import com.usian.jyzx.model.entity.user.UserAddress;
import com.usian.jyzx.model.entity.user.UserInfo;
import com.usian.jyzx.model.vo.common.Result;
import com.usian.jyzx.model.vo.common.ResultCodeEnum;
import com.usian.jyzx.user.service.UserAddressService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 07 12 14 22
 **/
@RestController
@RequestMapping("/api/user")
public class UserAddressController {
    @Autowired
    UserAddressService userAddressService;

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("/userAddress/auth/findUserAddressList")
    public Result selectUserDefaultAddress(HttpServletRequest request){
        String token = request.getHeader("token");
        String str =(String) redisTemplate.opsForValue().get("token:" + token);

        UserInfo userInfo = JSON.parseObject(str, UserInfo.class);
        Integer userId = userInfo.getId();
        List<UserAddress> userAddress = userAddressService.selectUserDefaultAddress(userId, null);
        return Result.build(ResultCodeEnum.SUCCESS.getCode(),
                ResultCodeEnum.SUCCESS.getMessage(),
                userAddress);

    }


    @GetMapping("/userAddress/auth/findUserAddressById/{addressId}")
    public UserAddress findUserAddressById(@PathVariable("addressId") Long addressId){
        return userAddressService.findUserAddressById(addressId);

    }
}
